package com.xbongbong.saas.script.tempenum;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.DefaultTypeEnum;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FormulaPoJo;
import com.xbongbong.paas.field.LinkedAttrPoJo;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.field.LinkKeyFieldInfoPojo;
import com.xbongbong.saas.enums.CustomerStatementProductEnum;
import com.xbongbong.saas.enums.ForbiddenSettingEnum;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.OutstockEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description
 * @author: shian
 * @date : 2021/1/26
 */
@Getter
public enum TempCustomerStatementProductEnum {
    PRODUCT(CustomerStatementProductEnum.PRODUCT),
    SELLING_PRICE(CustomerStatementProductEnum.SELLING_PRICE),
    NUM(CustomerStatementProductEnum.NUM),
    SUBTOTAL(CustomerStatementProductEnum.SUBTOTAL),
    ;

    private CustomerStatementProductEnum customerStatementProductEnum;

    TempCustomerStatementProductEnum(CustomerStatementProductEnum customerStatementProductEnum) {
        this.customerStatementProductEnum = customerStatementProductEnum;
    }

    public static void initProduct(FieldAttrEntity fieldAttrEntity, List<LinkedAttrPoJo> linkedAttrPoJoList, List<LinkedAttrPoJo> linkedShowPoJoList, PaasFormEntityExt paasFormEntity){
        LinkInfoPojo linkInfoPojo = new LinkInfoPojo();
        linkInfoPojo.setLinkBusinessType(XbbRefTypeEnum.PRODUCT.getCode());
        linkInfoPojo.setLinkedAttr(linkedAttrPoJoList);
        linkInfoPojo.setLinkSaasMark(SaasMarkEnum.SAAS.getCode());
        linkInfoPojo.setLinkKey(linkedAttrPoJoList.get(0).getAttr());
        linkInfoPojo.setLinkFormId(paasFormEntity.getId());
        linkInfoPojo.setLinkMenuId(paasFormEntity.getMenuId());
        linkInfoPojo.setLinkAppId(paasFormEntity.getAppId());
        linkInfoPojo.setCondition(new ArrayList<>());
        LinkKeyFieldInfoPojo linkKeyFieldInfoPojo = new LinkKeyFieldInfoPojo();
        BeanUtil.copyProperties(linkedAttrPoJoList.get(0),linkKeyFieldInfoPojo);
        linkInfoPojo.setLinkKeyAttr(linkKeyFieldInfoPojo);
        linkInfoPojo.setLinkedShowAttr(linkedShowPoJoList);
        fieldAttrEntity.setLinkInfo(linkInfoPojo);
        fieldAttrEntity.setShowEditable(1);
        fieldAttrEntity.setForbiddenSettingList(Arrays.asList(ForbiddenSettingEnum.DISABLE_VISIBLE_PERMISSION.getAlias(),ForbiddenSettingEnum.DISABLE_LINK_FORM_LINK_FIELD.getAlias()));
    }


    public static void initNum(FieldAttrEntity fieldAttrEntity){
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultValue(1);
        defaultAttrPoJo.setDefaultType(DefaultTypeEnum.CUSTOM.getAlias());
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setIsForbidden(1);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
        fieldAttrEntity.setShowEditable(1);
    }

    public static void initSubTotal(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setNumAccuracyAndScopeHide(1);
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "{self." + OutstockEnum.PRODUCT.getAttr() + "." + SelectProductEnum.SELLING_PRICE.getAttr() + "}*{self." + OutstockEnum.PRODUCT.getAttr() + "." + SelectProductEnum.NUM.getAttr() + "}";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + OutstockEnum.PRODUCT.getAttr() + "." + SelectProductEnum.SELLING_PRICE.getAttr(), "关联产品.售价");
        labelMap.put("self." + OutstockEnum.PRODUCT.getAttr() + "." + SelectProductEnum.NUM.getAttr(), "关联产品.数量");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_MONEY_TYPE.getAlias(),ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
    }
}
